﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Specification.Core;
using Domain.Entities;

namespace Domain.Specification.User
{
    /// <summary>
    /// 用户列表中查询参数的规约
    /// </summary>
    public class UserListSpecification : Specification<WebManageUsers>
    {
        #region Members

        DateTime? _FromDate = null;
        DateTime? _ToDate = null;
        int? _Status = null;
        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor for OrderDateSpecification
        /// </summary>
        /// <param name="fromDate">From date</param>
        /// <param name="toDate">To date</param>
        public UserListSpecification(DateTime? fromDate, DateTime? toDate, int? status)
        {
            _FromDate = fromDate;
            _ToDate = toDate;
            _Status = status;
        }

        #endregion

        #region Specification Overrides

        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Domain.Core.Specification.Specification{TEntity}"/>
        /// </summary>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Domain.Core.Specification.Specification{TEntity}"/></returns>
        public override System.Linq.Expressions.Expression<Func<WebManageUsers, bool>> SatisfiedBy()
        {
            Specification<WebManageUsers> spec = new TrueSpecification<WebManageUsers>();

            spec &= new DirectSpecification<WebManageUsers>(o => o.CreateDate > (_FromDate ?? DateTime.MinValue));

            spec &= new DirectSpecification<WebManageUsers>(o => o.CreateDate < (_ToDate ?? DateTime.MaxValue));

            if (_Status.HasValue)
                spec &= new DirectSpecification<WebManageUsers>(o => o.Status == (_Status.Value));

            return spec.SatisfiedBy();
        }

        #endregion
    }

}
